﻿
using log4net;
using Microsoft.Data.SqlClient.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "CfgFile/Log4Net_SqlServer.config", Watch = true)]

namespace PhysicalExamination5.Service
{
    public sealed class Logger
    {
        #region [ 单例模式 ]

        private static Logger logger;
        private static readonly ILog _Loggernet = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>  
        /// 无参私有构造函数  
        /// </summary>  
        private Logger()
        {

        }

        /// <summary>  
        /// 得到单例  
        /// </summary>  
        public static Logger Singleton
        {
            get
            {
                if (logger == null)
                {
                    logger = new Logger();
                }
                return logger;
            }
        }


        #endregion


        #region [ 参数 ]

        public bool IsDebugEnabled
        {
            get { return _Loggernet.IsDebugEnabled; }
        }
        public bool IsInfoEnabled
        {
            get { return _Loggernet.IsInfoEnabled; }
        }
        public bool IsWarnEnabled
        {
            get { return _Loggernet.IsWarnEnabled; }
        }
        public bool IsErrorEnabled
        {
            get { return _Loggernet.IsErrorEnabled; }
        }
        public bool IsFatalEnabled
        {
            get { return _Loggernet.IsFatalEnabled; }
        }


        #endregion


        #region [ 接口方法 ]


        #region [ Debug ]

        public void Debug(string message)
        {
            if (IsDebugEnabled)
            {
                Log(LogLevel.Debug, message);
            }
        }

        public void Debug(string message, Exception exception)
        {
            if (IsDebugEnabled)
            {
                Log(LogLevel.Debug, message, exception);
            }
        }

        public void DebugFormat(string format, params object[] args)
        {
            if (IsDebugEnabled)
            {
                Log(LogLevel.Debug, format, args);
            }
        }

        public void DebugFormat(string format, Exception exception, params object[] args)
        {
            if (IsDebugEnabled)
            {
                Log(LogLevel.Debug, string.Format(format, args), exception);
            }
        }


        #endregion


        #region [ Info ]

        public void Info(string message)
        {
            if (IsInfoEnabled)
            {
                Log(LogLevel.Info, message);
            }
        }

        public void Info(string message, Exception exception)
        {
            if (IsInfoEnabled)
            {
                Log(LogLevel.Info, message, exception);
            }
        }

        public void InfoFormat(string format, params object[] args)
        {
            if (IsInfoEnabled)
            {
                Log(LogLevel.Info, format, args);
            }
        }

        public void InfoFormat(string format, Exception exception, params object[] args)
        {
            if (IsInfoEnabled)
            {
                Log(LogLevel.Info, string.Format(format, args), exception);
            }
        }


        #endregion


        #region  [ Warn ]

        public void Warn(string message)
        {
            if (IsWarnEnabled)
            {
                Log(LogLevel.Warn, message);
            }
        }

        public void Warn(string message, Exception exception)
        {
            if (IsWarnEnabled)
            {
                Log(LogLevel.Warn, message, exception);
            }
        }

        public void WarnFormat(string format, params object[] args)
        {
            if (IsWarnEnabled)
            {
                Log(LogLevel.Warn, format, args);
            }
        }

        public void WarnFormat(string format, Exception exception, params object[] args)
        {
            if (IsWarnEnabled)
            {
                Log(LogLevel.Warn, string.Format(format, args), exception);
            }
        }


        #endregion


        #region  [ Error ]

        public void Error(string message)
        {
            if (IsErrorEnabled)
            {
                Log(LogLevel.Error, message);
            }
        }

        public void Error(string message, Exception exception)
        {
            if (IsErrorEnabled)
            {
                Log(LogLevel.Error, message, exception);
            }
        }

        public void ErrorFormat(string format, params object[] args)
        {
            if (IsErrorEnabled)
            {
                Log(LogLevel.Error, format, args);
            }
        }

        public void ErrorFormat(string format, Exception exception, params object[] args)
        {
            if (IsErrorEnabled)
            {
                Log(LogLevel.Error, string.Format(format, args), exception);
            }
        }

        #endregion


        #region  [ Fatal ]

        public void Fatal(string message)
        {
            if (IsFatalEnabled)
            {
                Log(LogLevel.Fatal, message);
            }
        }

        public void Fatal(string message, Exception exception)
        {
            if (IsFatalEnabled)
            {
                Log(LogLevel.Fatal, message, exception);
            }
        }

        public void FatalFormat(string format, params object[] args)
        {
            if (IsFatalEnabled)
            {
                Log(LogLevel.Fatal, format, args);
            }
        }

        public void FatalFormat(string format, Exception exception, params object[] args)
        {
            if (IsFatalEnabled)
            {
                Log(LogLevel.Fatal, string.Format(format, args), exception);
            }
        }

        #endregion


        #endregion


        #region [ 内部方法 ]  
        /// <summary>  
        /// 输出普通日志  
        /// </summary>  
        /// <param name="level"></param>  
        /// <param name="format"></param>  
        /// <param name="args"></param>  
        private void Log(LogLevel level, string format, params object[] args)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    _Loggernet.DebugFormat(format, args);
                    break;
                case LogLevel.Info:
                    _Loggernet.InfoFormat(format, args);
                    break;
                case LogLevel.Warn:
                    _Loggernet.WarnFormat(format, args);
                    break;
                case LogLevel.Error:
                    _Loggernet.ErrorFormat(format, args);
                    break;
                case LogLevel.Fatal:
                    _Loggernet.FatalFormat(format, args);
                    break;
            }
        }

        /// <summary>  
        /// 格式化输出异常信息  
        /// </summary>  
        /// <param name="level"></param>  
        /// <param name="message"></param>  
        /// <param name="exception"></param>  
        private void Log(LogLevel level, string message, Exception exception)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    _Loggernet.Debug(message, exception);
                    break;
                case LogLevel.Info:
                    _Loggernet.Info(message, exception);
                    break;
                case LogLevel.Warn:
                    _Loggernet.Warn(message, exception);
                    break;
                case LogLevel.Error:
                    _Loggernet.Error(message, exception);
                    break;
                case LogLevel.Fatal:
                    _Loggernet.Fatal(message, exception);
                    break;
            }
        }

        #endregion
    }


    #region [ enum: LogLevel ]

    /// <summary>  
    /// 日志级别  
    /// </summary>  
    public enum LogLevel
    {
        Debug,
        Info,
        Warn,
        Error,
        Fatal
    }


    #endregion
}
